package com.ruoyi.web.controller.task;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.task.domain.SysTask;
import com.ruoyi.task.service.ISysTaskService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务Controller
 * 
 * @author yjf
 * @date 2022-06-30
 */
@RestController
@RequestMapping("/task")
public class SysTaskController extends BaseController
{
    @Autowired
    private ISysTaskService sysTaskService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTask sysTask)
    {
        Long userId = SecurityUtils.getUserId();
        if (SysUser.isAdmin(userId)) {
        } else if (UserType.CONSERVATOR.getValue().equals(SecurityUtils.getUserType())) {
            // 管理员显示所有本级以下的内容
            Long deptId = SecurityUtils.getDeptId();
            List<Long> deptIds = deptService.selectNormalDeptById(deptId);
            if (StringUtils.isEmpty(deptIds)) {
                return null;
            }
            sysTask.setDeptIdsList(deptIds);
        } else {
            sysTask.setUserId(userId);
        }

        startPage();
        List<SysTask> list = sysTaskService.selectSysTaskList(sysTask);
        return getDataTable(list);
    }

    /**
     * 获取任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId)
    {
        return AjaxResult.success(sysTaskService.selectSysTaskByTaskId(taskId));
    }

    /**
     * 新增任务
     */
    @PreAuthorize("@ss.hasPermi('task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTask sysTask)
    {
        return toAjax(sysTaskService.insertSysTask(sysTask));
    }

    /**
     * 复制任务
     */
    @PreAuthorize("@ss.hasPermi('task:add')")
    @Log(title = "复制任务", businessType = BusinessType.INSERT)
    @PostMapping("/cloneTask")
    public AjaxResult cloneTask(@RequestBody SysTask sysTask)
    {
        return toAjax(sysTaskService.cloneSysTask(sysTask));
    }

    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasPermi('task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysTask sysTask)
    {
        return toAjax(sysTaskService.updateSysTask(sysTask));
    }

    /**
     * 修改任务启用状态
     */
    @PreAuthorize("@ss.hasPermi('task:edit')")
    @Log(title = "修改任务启用状态", businessType = BusinessType.UPDATE)
    @PostMapping("/changeTaskStatus")
    public AjaxResult changeTaskStatus(@RequestBody SysTask sysTask)
    {
        return toAjax(sysTaskService.changeTaskStatus(sysTask));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds)
    {
        return toAjax(sysTaskService.deleteSysTaskByTaskIds(taskIds));
    }
}
